﻿using DoNetDrive.Common.Extensions;
using DoNetDrive.Protocol.Door.Door8800;
using DoNetDrive.Protocol.Util;
using DotNetty.Buffers;
using System;
using System.Collections.Generic;
using System.Text;

namespace DoNetDrive.Protocol.Fingerprint.SystemParameter
{
    /// <summary>
    /// 设置访客根密码命令的参数
    /// </summary>
    public class WriteGuestPassword_Parameter : AbstractParameter
    {

        /// <summary>
        /// 访客根密码
        /// </summary>
        public string GuestPassword { get; set; }


        /// <summary>
        /// 构建一个空的第三方平台推送功能实例,默认禁用所有
        /// </summary>
        public WriteGuestPassword_Parameter()
        {
            GuestPassword = string.Empty;
        }

        /// <summary>
        /// 创建设置第三方平台推送功能的参数
        /// </summary>
        /// <param name="pwd">访客根密码</param>
        public WriteGuestPassword_Parameter(string pwd)
        {
            GuestPassword = pwd;
        }

        /// <summary>
        /// 检查参数
        /// </summary>
        /// <returns></returns>
        public override bool checkedParameter()
        {
            if (string.IsNullOrWhiteSpace(GuestPassword))
            {
                GuestPassword = string.Empty;
            }

            if (string.IsNullOrEmpty(GuestPassword))
            {
                GuestPassword = string.Empty;
            }
            else
            {
                if (!GuestPassword.IsNum())
                {
                    return false;
                }

                if (GuestPassword.Length < 4 || GuestPassword.Length > 8)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 获取数据长度
        /// </summary>
        /// <returns></returns>
        public override int GetDataLen()
        {
            return 4;
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public override void Dispose()
        {
            return;
        }

        /// <summary>
        /// 编码参数
        /// </summary>
        /// <param name="databuf"></param>
        /// <returns></returns>
        public override IByteBuffer GetBytes(IByteBuffer databuf)
        {
            string sPWD = "FFFFFFFF";
            if (!string.IsNullOrEmpty(GuestPassword))
            {
                sPWD = GuestPassword;
            }
            if (sPWD.Length < 8)
            {
                sPWD = sPWD + new string('F', 8 - sPWD.Length);
            }
            byte[] buf = sPWD.HexToByte();

            databuf.WriteBytes(buf);

            return databuf;
        }

        /// <summary>
        /// 解码参数
        /// </summary>
        /// <param name="databuf"></param>
        public override void SetBytes(IByteBuffer databuf)
        {

            byte[] buf = new byte[4];
            databuf.ReadBytes(buf, 0, 4);

            string sHex = buf.ToHex();
            GuestPassword = sHex.Replace("F", string.Empty);
        }
    }
}
